package cn.kgc.tangcco.pojo;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
 * @author 李昊哲
 * @Description
 * @create 2021/1/16 17:09
 */
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Accessors(chain = true)
@Table(name = "stu")
@Entity
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
public class Stu implements Serializable {
    private static final long serialVersionUID = -4089830172842590362L;
    /**
     * 学生编号
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    /**
     * 学生姓名
     */
    @Column(name = "nickname")
    private String nickname;

    public Stu(String nickname) {
        this.nickname = nickname;
    }

    public Stu(String nickname, List<Course> courses) {
        this.nickname = nickname;
        this.courses = courses;
    }

    /**
     * 该学生所学专业列表
     * joinColumns当前类的主键
     * inverseJoinColumns关联对象的主键
     * mappedBy不可与JoinTable和JoinColumn一同使用
     */
    @JsonBackReference
    @ManyToMany(targetEntity = Course.class, fetch = FetchType.LAZY)
    @JoinTable(
            name = "sc",
            joinColumns = {@JoinColumn(name = "sid", referencedColumnName = "id")},
            inverseJoinColumns = @JoinColumn(name = "cid", referencedColumnName = "id"))
    private List<Course> courses;
}
